﻿@using FlexFlow.Model;
@using FlexFlow.Runtime;

@{
    Layout = null;
}
@{
    ApplicationDefine[] apps = (ApplicationDefine[])WorkFlowEngine.Default.GetRegisteredApplications().Tag;
}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
    <title>编辑流程定义</title>
    <link href="~/Assets/css/blockUI.css" rel="stylesheet" />
    <link href="~/Assets/css/gooFlow/GooFlow.css" rel="stylesheet" />
    <link href="~/Assets/css/bootstrap.min.css" rel="stylesheet" />
    <link href="~/Assets/css/ace.min.css" rel="stylesheet" />
    <link href="~/Assets/css/ace-rtl.min.css" rel="stylesheet" />
    <link href="~/Assets/css/ace-skins.min.css" rel="stylesheet" />
    <link href="~/Assets/css/context.css" rel="stylesheet" />

    <script src="~/Assets/js/jquery-2.0.3.min.js"></script>
    <script src="~/Assets/js/bootstrap.min.js"></script>
    <script src="~/Assets/js/bootbox.min.js"></script>
    <script src="~/Assets/js/flexflow-1.0b.js"></script>
    <script src="~/Assets/js/gooFlow/GooFunc.js"></script>
    <script src="~/Assets/js/gooFlow/json2.js"></script>
    <script src="~/Assets/js/gooFlow/GooFlow.js"></script>
    <script src="~/Assets/js/jquery.blockUI.js"></script>
    <script src="~/Assets/js/context.js"></script>

    <style>
        /*修复typeahead的预选提示文本框的样式*/
        input.tm-input-typeahead[readonly] {
            cursor: default;
            background: #ffffff none repeat scroll 0% 0% !important;
        }
    </style>
</head>
<body>
    <!--[if lt IE 9]>
    <?import namespace="v" implementation="#default#VML" ?>
    <![endif]-->
    <div id="loader" style="display:none;">
        <div class="spinner">
        </div>
    </div>
    <div id="processEditArea" style="margin:10px"></div>
    <script type="text/javascript">
        var fileId = '@Request["id"]';
        var property = {
            width: 1200,
            height: 600,
            toolBtns: [//"start round", "end round", "task round", "node", "chat", "state", "plug", "join", "fork", "complex mix"],
                @for (int i = 0; i < apps.Length; i++)
			    {
                    ApplicationDefine app = apps[i];
                    @:'@app.ActivityType @app.CssClass'
                    if(i<apps.Length-1)
                    {
                        @:,
                    }
			    }
            ],
            haveHead: true,
            headBtns: ["new", "open", "edit", "save", "undo", "redo", "reload"],//如果haveHead=true，则定义HEAD区的按钮
            haveTool: true,
            haveGroup: true,
            useOperStack: true
        };
        var remark = {
            cursor: "选择指针",
            direct: "结点连线",
            @for (int i = 0; i < apps.Length; i++)
            {
                ApplicationDefine app = apps[i];
                @:@app.ActivityType:'@app.ActivityDescription'
                if(i<apps.Length-1)
                {
                    @:,
                }
            }
            //start: "入口结点",
            //"end": "结束结点",
            //"task": "任务结点",
            //node: "自动结点",
            //chat: "决策结点",
            //state: "状态结点",
            //plug: "附加插件",
            //fork: "分支结点",
            //"join": "联合结点",
            //"complex mix": "复合结点",
            //group: "组织划分框编辑开关"
        };
        var procEditor;
        var selNode;
        $(document).ready(function () {
            procEditor = $.createGooFlow($("#processEditArea"), property);
            procEditor.setNodeRemarks(remark);
            procEditor.onItemDel = function (id, type) {
                return confirm("确定要删除该单元吗?");
            };
            procEditor.onBtnSaveClick = function () {
                doOperation({
                    'url': '/ProcessDefineAPI/Save',
                    'data': {
                        'fileId': fileId,
                        'content': JSON.stringify(procEditor.exportData())
                    },
                    'success': function (d) {
                        bootbox.alert("保存成功！");
                    }
                });
            };
            procEditor.onBtnEditClick = function () {
                var selId = procEditor.$focus;
                if (selId == "") {
                    bootbox.alert("请先选择一个节点或连线！");
                }
                else {
                    selNode = procEditor.getItemInfo(selId, "node");
                    if (selNode == null) {
                        selNode = procEditor.getItemInfo(selId, "line");
                        if (selNode == null) {
                            bootbox.alert("选定节点不支持编辑或已被移除！");
                            return;
                        }
                    }
                    if (!selNode.opt) {
                        selNode.opt = {};
                    }
                    showEdit();
                }

            }

            //预加载数据
            doOperation({
                'url': '/ProcessDefineAPI/Load',
                'data': {
                    'fileId': fileId,
                },
                'success': function (d) {
                    procEditor.loadData(eval('(' + d.Tag + ')'));
                    document.title = "编辑流程定义-" + procEditor.$title;
                }
            });
            var winSize = findDimensions();//调用函数，获取数值
            procEditor.reinitSize(winSize.winWidth - 20, winSize.winHeight - 20);
            $(window).on('resize', function (e) {
                var winSize = findDimensions();//调用函数，获取数值
                procEditor.reinitSize(winSize.winWidth - 20, winSize.winHeight - 20);
            });
            //demo.loadData(jsondata);

        });
        function showEdit() {
            $.ajax({
                type: 'GET',
                url: "/ProcessDefine/EditNode?nodeType=" + selNode.type.split(' ')[0],
                success: function (data) {
                    bootbox.dialog({
                        message: data,
                        title: "编辑节点",
                        buttons: {
                            ok: {
                                label: "完成",
                                className: "btn-success",
                                callback: function () {
                                    saveForm();
                                }
                            },
                            cancel: {
                                label: "取消",
                                className: "btn-default"
                            }
                        }
                    }
                    );
                }
            });
        }
    </script>

</body>
</html>